package org.example.service.imp;

import org.apache.ibatis.session.SqlSession;
import org.example.bean.ClassBean;
import org.example.mapper.IClassMapper;
import org.example.service.IClassService;

import java.time.LocalDate;
import java.util.List;

public class ClassServiceImp extends BaseService implements IClassService{

    @Override
    public List<ClassBean> findAll() {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        List<ClassBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public boolean add(ClassBean classBean) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        ClassBean classBean1 = mapper.findByClassName(classBean.getClassName());
        if (classBean1 == null){
            mapper.add(classBean);
            session.commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }

    @Override
    public boolean del(Integer classID) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        Integer num = mapper.countStudentByClassID(classID);
        if (num==null){
            mapper.del(classID);
            session.commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }
}
